package com.jereh.topnews.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.jereh.topnews.R;
import com.jereh.topnews.adapter.NewsListAdapter;
import com.jereh.topnews.entity.News;

import java.util.ArrayList;
import java.util.List;

public class NewsListActivity extends AppCompatActivity {

    private ListView listView;
    private BaseAdapter adapter;
    private List<News> list=new ArrayList<>();
    private View footerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_demo);
        listView=(ListView)findViewById(R.id.list);
        footerView= LayoutInflater.from(this).inflate(R.layout.view_footer_listview,null);
        listView.addFooterView(footerView);
//        adapter=new NewsListAdapter(this,list);
        listView.setAdapter(adapter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == SCROLL_STATE_IDLE) {
                    if (view.getLastVisiblePosition() == list.size()) {
                        updateData();
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        updateData();
    }
    private void updateData() {
//        final Handler handler=new Handler();
        final Runnable callback=new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        };
        final Handler handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 1:
                        adapter.notifyDataSetChanged();
                        break;
                }
            }

        };
        Thread thread=new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                setData();
//                handler.sendEmptyMessage(1);
                runOnUiThread(callback);
            }
        };
        thread.start();
    }
    private void setData() {
        News news1=new News();
        news1.setTitle("普京又特批一人入籍，这次是好莱坞打星");
        news1.setImg1(R.mipmap.news_pic1);
        news1.setAuthor("人民日报");
        news1.setPicCount(6);
        news1.setReply(123);
        news1.setType(NewsListAdapter.TYPE_BIG_PIC);
        list.add(news1);
        News news2=new News();
        news2.setTitle("FBI公布克林顿赦免逃亡富豪调查文件 民主党又怒了");
        news2.setImg1(R.mipmap.news_pic1);
        news2.setImg1(R.mipmap.news_pic2);
        news2.setImg1(R.mipmap.news_pic3);
        news2.setAuthor("人民日报");
        news2.setReply(123);
        news2.setDate("八小时前");
        news2.setType(NewsListAdapter.TYPE_PIC);
        list.add(news2);
        News news3=new News();
        news3.setTitle("美国大选倒数5天：情势紧绷 仍有选民将改变心意");
        news3.setAuthor("光明网");
        news3.setReply(3223);
        news3.setDate("刚刚");
        news3.setType(NewsListAdapter.TYPE_NO_PIC);
        list.add(news3);
        News news6=new News();
        news6.setTitle("美国大选倒数5天：情势紧绷 仍有选民将改变心意");
        news6.setAuthor("光明网");
        news6.setReply(3223);
        news6.setDate("刚刚");
        news6.setType(NewsListAdapter.TYPE_NO_PIC);
        list.add(news6);
        News news5=new News();
        news5.setTitle("FBI公布克林顿赦免逃亡富豪调查文件 民主党又怒了");
        news5.setImg1(R.mipmap.news_pic1);
        news5.setImg1(R.mipmap.news_pic2);
        news5.setImg1(R.mipmap.news_pic3);
        news5.setAuthor("人民日报");
        news5.setReply(123);
        news5.setDate("八小时前");
        news5.setType(NewsListAdapter.TYPE_PIC);
        list.add(news5);
        News news4=new News();
        news4.setTitle("普京又特批一人入籍，这次是好莱坞打星");
        news4.setImg1(R.mipmap.news_pic1);
        news4.setAuthor("人民日报");
        news4.setPicCount(6);
        news4.setReply(123);
        news4.setType(NewsListAdapter.TYPE_BIG_PIC);
        list.add(news4);

    }
}
